/* GameManager.java
 * 
 * Alfred Olschnoegger
 * 23.05.2008
 */

package domain.game;

import java.awt.geom.Area;
import java.util.Collection;
import java.util.HashMap;

import domain.location.GameArea;

/**
 * Manages the Games of the server.
 *
 * @date 23.05.2008
 * @author Alfred Olschnoegger
 */

public class GameManager {
	
	// Singleton Methods & Members
	private static GameManager s_Instance;
	
	public static GameManager getInstance() {
		if(s_Instance == null) {
			s_Instance = new GameManager();
		}
		return s_Instance;
	}
	
	
	// Objects things
	
	private int m_MaxGameCount = 10;
	private int m_LastGameID = 0;
	private int m_GameCount = 0;
	private HashMap<Integer,Game> m_Games = new HashMap<Integer,Game>();
	
	private GameManager() {}
	
	/**
	 * creates a new game
	 * 
	 * @param name name of the game
	 * @param time duration of the game
	 * @param area area of the game in minutes
	 * @return the new game
	 */
	public synchronized Game getNewGame(String name, int time, GameArea area) {
		if(m_MaxGameCount > m_GameCount) {
			m_GameCount++;
			m_LastGameID++;
			m_Games.put(m_LastGameID, new Game(name, m_LastGameID, time, area));
			return m_Games.get(m_LastGameID);
		}
		return null;
	}
	
	/**
	 * 
	 * @param gameID the game id of the searched game
	 * @return returns the game with the given gameID
	 */
	public synchronized Game getGame(int gameID) {
		if(m_Games.containsKey(gameID)) {
			return m_Games.get(gameID);
		}
		return null;
	}
	
	/**
	 * 
	 * @param gameID ends a game
	 */
	protected void gameEnded(int gameID) {
		if(m_Games.containsKey(gameID)) {
			m_GameCount--;
		}
	}

	/**
	 * @return collection of all games
	 */
	public Collection<Game> getGameList() {
		return m_Games.values();
	}
	
	public boolean stopGame(int gameID) {
		if(m_Games.containsKey(gameID)) {
			Game g = m_Games.get(gameID);
			g.end();
			return true;
		}
		return false;
	}
	
	

}


// end of GameManager.java
